﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.ComponentModel;
using snat.model;

namespace gui {
    /// <summary>
    /// Interaction logic for GraphicsTab.xaml (Visualisation Tab)
    /// </summary>
    public partial class GraphicsTab : UserControl {

        public static event guiRelayoutEvent RelayoutEvent;
        public static event guiCompactLayoutEvent CompactLayoutEvent;
        public static event guiExpandLayoutEvent ExpandLayoutEvent;

        private ClusterNode nodeSelected;

        public GraphicsTab() {
            InitializeComponent();

            // Set up data binding
            GraphStats._Dispatcher = this.Dispatcher;
            this.DataContext = GraphStats.Instance;

            // Set visualisation to default off
            NetworkModelConnect.Instance.RunWithVisuals = false;

            // Register graph events
            NetworkModel.Instance.EdgeAdded += guiOnEdgeAdded;
            Graph.GraphNodeSelectedEvent += guiOnNodeSelected;

            // Register check box enable/disable events
            NetworkModelConnect.OnFinishedProcessingEvent += EnableCheckBoxes;
            NetworkModelConnect.OnConnectedEvent += DisableCheckBoxes;
            NetworkModelConnect.TapeLoadedEvent += EnableCheckBoxes;
            PlayControl.RunningEvent += DisableCheckBoxes;
            PlayControl.ReadyEvent += EnableCheckBoxes;
            PlayControl.FinishedEvent += EnableCheckBoxes;
        }

        // Call to Update the Graph on new nodes/edges
         public void guiOnEdgeAdded(Edge e) { 
            if(nodeSelected != null) GraphStats.PopulateWithNode(nodeSelected);
        }

        public void guiOnNodeSelected(ClusterNode b) {
            nodeSelected = b;
            GraphStats.PopulateWithNode(b);
        }

        private void ApplyUpdateLayout(object sender, RoutedEventArgs e) {
            StatusInfo.SetStatus("Calculating new graph layout");
            if (RelayoutEvent != null) RelayoutEvent(); // Perform layout shuffle by firing a relayout event
        }

        private void RunWithVisualisation(object sender, RoutedEventArgs e) {
            NetworkModelConnect.Instance.RunWithVisuals = true;
        }

        private void RunWithOutVisualisation(object sender, RoutedEventArgs e) {
            NetworkModelConnect.Instance.RunWithVisuals = false;
        }

        private void CompactNodeLayout(object sender, RoutedEventArgs e) {
            if (CompactLayoutEvent != null) CompactLayoutEvent(); // Update graph LayoutParams via event
        }

        private void ExpandNodeLayout(object sender, RoutedEventArgs e) {
            if (ExpandLayoutEvent != null) ExpandLayoutEvent(); // Update graph LayoutParams via event
        }

        private void EnableCheckBoxes() {
            RunWithVisXAML.IsEnabled = true;
            UseCompactXAML.IsEnabled = true;
        }

        private void DisableCheckBoxes() {
            RunWithVisXAML.IsEnabled = false;
            UseCompactXAML.IsEnabled = false;
        }


    }

}
